package com.xyoto.community.service.impl;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.constant.DatabaseOperationResult;
import com.xyoto.community.dto.DeleteProverbDTO;
import com.xyoto.community.dto.ProverbDTO;
import com.xyoto.community.entity.main.Proverb;
import com.xyoto.community.enums.ProverbEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.ProverbMapper;
import com.xyoto.community.service.ProverbService;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.vo.ProverbVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Author: 廖振辉
 * Date: 2021/11/17
 * Description:
 */
@Service
public class ProverbServiceImpl implements ProverbService {

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    @Autowired
    private ProverbMapper proverbMapper;

    /**
     * 获取一条每日金句
     * @author 廖振辉
     * @return {@link Result}
     */
    @Override
    public Result getOne() {
        // 从redis中拿
        Object proverbFromCache = redisCommonUtils.get(CacheInstruction.PROVERB);
        Proverb proverb = null;
        if (proverbFromCache == null){
            // 从数据中取出一条
            proverbFromCache = proverbMapper.selectOneByRandom();
            if(proverbFromCache == null){
                // 数据库中无金句
                return Result.success(new ProverbVO("欢迎来到O2社区","氧社区开发组"));
            }
            proverb = (Proverb) proverbFromCache;
            // 更新redis
            updateProverbInCache(proverb);
        }
        proverb = (Proverb) proverbFromCache;
        return Result.success(new ProverbVO(proverb.getSentence(),proverb.getAuthor()));
    }

    /**
     * 新增每日金句
     * @author 廖振辉
     * @param proverbDTO
     * @return {@link Result}
     */
    @Override
    public Result addOne(ProverbDTO proverbDTO) {
        Integer insert = proverbMapper.insertOne(new Proverb(proverbDTO.getSentence(),proverbDTO.getAuthor()));
        if (insert < DatabaseOperationResult.SUCCESS){
            ExceptionUtils.error(ProverbEnum.ADD_ERROR);
        }
        return Result.success();
    }

    /**
     * 根据金句id删除金句
     * @author 廖振辉
     * @param deleteProverbDTO
     * @return {@link Result}
     */
    @Override
    public Result deleteById(DeleteProverbDTO deleteProverbDTO) {
        Proverb proverb = proverbMapper.selectOneById(deleteProverbDTO.getId());
        if (proverb == null){
            // 金句不存在
            ExceptionUtils.error(ProverbEnum.PROVERB_IS_NULL);
        }
        Integer delete = proverbMapper.deleteById(deleteProverbDTO.getId());
        if (delete < DatabaseOperationResult.SUCCESS){
            // 从数据库删除失败
            ExceptionUtils.error(ProverbEnum.DELETE_ERROR);
        }
        return Result.success();
    }

    /**
     * 更新redis中的每日金句
     * @author 廖振辉
     * @param proverb 金句
     * @return {@link }
     */
    private synchronized void updateProverbInCache(Proverb proverb) {
        Object proverbFromCache = redisCommonUtils.get(CacheInstruction.PROVERB);
        if (proverbFromCache == null){
            redisCommonUtils.set(CacheInstruction.PROVERB,proverb,CacheInstruction.PROVERB_EXPIRE_TIME, TimeUnit.SECONDS);
        }
    }
}
